const { Service } = require('egg')
const { tableToList } = require('../utils/tools')

module.exports = class ExamService extends Service {
  /**
   * ! 考试安排 没查到数据
   * @param {String} term 学期
   * @return {Array} *
   */
  async table(term) {
    const Cookie = this.ctx.state.Cookie
    const apiUrl = `${this.ctx.state.baseUrl}/jsxsd/xsks/xsksap_list`
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      data: {
        xqlbmc: '',
        xnxqid: term,
        xqlb: '',
      },
      timeout: 10000,
    })
    const header = ['课程名称', '考试时间', '考场', '座位号'] // 列名称
    if (res.status === 200) {
      try {
        const tableList = tableToList(res.data.toString(), '#dataList')
        return { tableList, header }
      } catch (err) {
        console.log('err', err)
        throw err
      }
    }
    return { table: [], header }
  }
}
